using System;
using System.Collections;

using Microsoft.MediaCenter.UI;
using GalleryItem = McePicasa.GalleryItem;

namespace McePicasa
{
    /// <summary>
    /// This object contains the standard set of information displayed in the 
    /// gallery page UI.
    /// </summary>
    public class GalleryPage : FilteredContentPage
    {
        public GalleryPage()
        {
        }

        public GalleryPage(GalleryItem item)
        {
            this.Owner = item;
            this.isAlbumPage = true;
        }

        public GalleryPage(MenuCategory category)
        {
            this.Owner = category;
        }

        /// <summary>
        /// The desired size for a galley item.  This will be interpreted by the 
        /// UI to control what size to allow for the thumbnails as well as how 
        /// many rows to display.
        /// </summary>
        public GalleryItemSize ItemSize
        {
            get { return itemSize; }
            set { itemSize = value; }
        }

        public void GoUp()
        {
            if (Owner == null)
                Application.Current.MessageBox("Owner == null", "GoUp");
            else if (Owner is MenuCategory)
                Application.Current.GoToMenu();
            else if (!(Owner is GalleryItem))
                Application.Current.MessageBox("Owner is not MenuCategory/GalleryItem", "GoUp");
            else if ((Owner as GalleryItem).Owner == null)
                Application.Current.MessageBox("Owner.Owner == null", "GoUp");
            else if ((Owner as GalleryItem).Owner is GalleryPage)
                Application.Current.GoToGallery((Owner as GalleryItem).Owner as GalleryPage);
            else if ((Owner as GalleryItem).Owner is MenuCategory)
                Application.Current.GoToGallery(Application.Current.CurrentUser.CreateAlbumGalleryPage(
                    (Owner as GalleryItem).Owner as MenuCategory));
            else if ((Owner as GalleryItem).Owner is SearchPage)
                Application.Current.GoToSearch((Owner as GalleryItem).Owner as SearchPage);
            else
                Application.Current.MessageBox("Owner.Owner is Unknown", "GoUp");
        }

        private GalleryItemSize itemSize;

        #region Slideshow

        /// <summary>
        /// This method is to invoke slide show when user clicks the Slideshow
        /// button on the mclm page
        /// </summary>
        public void InvokeSlideshow()
        {
            PicasaAlbum picasaAlbum = ((GalleryItem)this.Owner).Album;
            Timer slideshowTimer = new Timer();
            slideshowTimer.Data.Add("index", 0);
            slideshowTimer.Data.Add("PicasaAlbum", picasaAlbum);
            slideshowTimer.Interval = 3000;
            slideshowTimer.Tick += new EventHandler(SlideShowExecute);
            DetailsPage.SlideshowTimer = slideshowTimer;
            DetailsPage.IsPlayed = true;
            SlideShowExecute(slideshowTimer, null);
            slideshowTimer.Start();
        }

        /// <summary>
        /// This private method is to execute slide show after a specified
        /// interval of the timer.
        /// </summary>
        private void SlideShowExecute(object sender, EventArgs e)
        {
            try
            {
                Timer ssTimer = sender as Timer;
                PicasaAlbum picasaAlbum = (PicasaAlbum)ssTimer.Data["PicasaAlbum"];
                int index = (int)ssTimer.Data["index"] % picasaAlbum.NoOfPhotos;

                PicasaPhoto picasaPhoto = picasaAlbum.GetPhoto(index++);
                ssTimer.Data["index"] = index;
                Application.Current.GoToDetails(Application.Current.CurrentUser.CreatePhotoDetailsPage(new GalleryItem(this, picasaPhoto), true));
            }
            catch (InvalidOperationException ex)
            {
                Log.LogIt("SlideShowExecute: InvalidOperationException, " + ex.Message);
                //Global.MsgBox("SlideShowExecute: " + ex.Message, 3, false);
            }
        }

        public bool IsAlbumPage
        {
            get { return isAlbumPage; }
        }

        private bool isAlbumPage;

        #endregion
    }



    /// <summary>
    /// This object contains the standard set of information displayed in the 
    /// gallery page UI. FLICKR
    /// </summary>
    public class GalleryPageFlickr : FilteredContentPage
    {
        public GalleryPageFlickr()
        {
        }

        public GalleryPageFlickr(GalleryItemFlickr item)
        {
            this.Owner = item;
            this.isAlbumPage = true;
        }

        public GalleryPageFlickr(MenuCategory category)
        {
            this.Owner = category;
        }

        /// <summary>
        /// The desired size for a galley item.  This will be interpreted by the 
        /// UI to control what size to allow for the thumbnails as well as how 
        /// many rows to display. 
        /// </summary>
        public GalleryItemSize ItemSize
        {
            get { return itemSize; }
            set { itemSize = value; }
        }

        public void GoUp()
        {
            if (Owner == null)
                Application.Current.MessageBox("Owner == null", "GoUp");
            else if (Owner is MenuCategory)
                Application.Current.GoToMenu();
            else if (!(Owner is GalleryItemFlickr))
                Application.Current.MessageBox("Owner is not MenuCategory/GalleryItem", "GoUp");
            else if ((Owner as GalleryItemFlickr).Owner == null)
                Application.Current.MessageBox("Owner.Owner == null", "GoUp");
            else if ((Owner as GalleryItemFlickr).Owner is GalleryPageFlickr)
                Application.Current.GoToGallery((Owner as GalleryItemFlickr).Owner as GalleryPageFlickr);
            else if ((Owner as GalleryItemFlickr).Owner is MenuCategory)
                Application.Current.GoToGallery(Application.Current.CurrentUser.CreateAlbumGalleryPage(
                    (Owner as GalleryItemFlickr).Owner as MenuCategory));
            else if ((Owner as GalleryItemFlickr).Owner is SearchPage)
                Application.Current.GoToSearch((Owner as GalleryItemFlickr).Owner as SearchPage);
            else
                Application.Current.MessageBox("Owner.Owner is Unknown", "GoUp");
        }

        private GalleryItemSize itemSize;

        #region Slideshow

        /// <summary>
        /// This method is to invoke slide show when user clicks the Slideshow
        /// button on the mclm page
        /// </summary>
        public void InvokeSlideshow()
        {
            //PicasaAlbum picasaAlbum = ((GalleryItem)this.Owner).Album;
            //Timer slideshowTimer = new Timer();
            //slideshowTimer.Data.Add("index", 0);
            //slideshowTimer.Data.Add("PicasaAlbum", picasaAlbum);
            //slideshowTimer.Interval = 3000;
            //slideshowTimer.Tick += new EventHandler(SlideShowExecute);
            //DetailsPage.SlideshowTimer = slideshowTimer;
            //DetailsPage.IsPlayed = true;
            //SlideShowExecute(slideshowTimer, null);
            //slideshowTimer.Start();
        }

        /// <summary>
        /// This private method is to execute slide show after a specified
        /// interval of the timer.
        /// </summary>
        private void SlideShowExecute(object sender, EventArgs e)
        {
            try
            {
                Timer ssTimer = sender as Timer;
                PicasaAlbum picasaAlbum = (PicasaAlbum)ssTimer.Data["PicasaAlbum"];
                int index = (int)ssTimer.Data["index"] % picasaAlbum.NoOfPhotos;

                PicasaPhoto picasaPhoto = picasaAlbum.GetPhoto(index++);
                ssTimer.Data["index"] = index;
                Application.Current.GoToDetails(Application.Current.CurrentUser.CreatePhotoDetailsPage(new GalleryItem(this, picasaPhoto), true));
            }
            catch (InvalidOperationException ex)
            {
                Log.LogIt("SlideShowExecute: InvalidOperationException, " + ex.Message);
                //Global.MsgBox("SlideShowExecute: " + ex.Message, 3, false);
            }
        }

        public bool IsAlbumPage
        {
            get { return isAlbumPage; }
        }

        private bool isAlbumPage;

        #endregion
    }





    /// <summary>
    /// The possible size configurations supported by the gallery UI.
    /// </summary>
    public enum GalleryItemSize
    {
        Small,
        Large
    }
}
